{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 1、导入模块与读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['continentName' 'continentEnglishName' 'countryName' 'countryEnglishName'\n",
      " 'provinceName' 'provinceEnglishName' 'province_zipCode'\n",
      " 'province_confirmedCount' 'province_suspectedCount' 'province_curedCount'\n",
      " 'province_deadCount' 'cityName' 'cityEnglishName' 'city_zipCode'\n",
      " 'city_confirmedCount' 'city_suspectedCount' 'city_curedCount'\n",
      " 'city_deadCount' 'updateTime']\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "country_index=np.array(open('china_nCov.csv').readline()[:-1].split(\",\"))\n",
    "print(country_index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 2、缺失值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def isNone(str):\n",
    "    if len(str)==0:\n",
    "        return 0\n",
    "    return str"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 3、加载数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['河南省' '1231' '0' ... '74' '2' '02-16-2020']\n",
      " ['河南省' '1231' '0' ... '57' '2' '02-16-2020']\n",
      " ['河南省' '1231' '0' ... '72' '0' '02-16-2020']\n",
      " ...\n",
      " ['香港' '0' '117' ... '0' '0' '01-22-2020']\n",
      " ['黑龙江省' '0' '1' ... '0' '0' '01-22-2020']\n",
      " ['湖南省' '1' '0' ... '0' '0' '01-22-2020']]\n"
     ]
    }
   ],
   "source": [
    "params=dict(\n",
    "    fname='china_nCov.csv',\n",
    "    delimiter=',',\n",
    "    skiprows=1,\n",
    "    dtype=np.str,\n",
    "    usecols=(4,7,8,9,10,11,14,15,16,17,18),\n",
    "    converters={\n",
    "        7:isNone,\n",
    "        8:isNone,\n",
    "        9:isNone,\n",
    "        10:isNone,\n",
    "        14:isNone,\n",
    "        15:isNone,\n",
    "        16:isNone,\n",
    "        17:isNone        \n",
    "    }\n",
    ")\n",
    "data=np.loadtxt(**params)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 4、数据的查找与去重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['河南省' '1231' '0' ... '74' '2' '02-16-2020']\n",
      " ['河南省' '1231' '0' ... '57' '2' '02-16-2020']\n",
      " ['河南省' '1231' '0' ... '72' '0' '02-16-2020']\n",
      " ...\n",
      " ['上海市' '326' '0' ... '0' '0' '02-16-2020']\n",
      " ['上海市' '326' '0' ... '3' '0' '02-16-2020']\n",
      " ['上海市' '326' '0' ... '2' '0' '02-16-2020']]\n"
     ]
    }
   ],
   "source": [
    "# (1)查找2020年2月16日的全国疫情数据。\n",
    "time='02-16-2020'\n",
    "timedata=data[np.where(data==time)[0]]\n",
    "print(timedata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['湖北省' '13522' '0' ... '2' '18' '02-04-2020']\n",
      " ['湖北省' '9074' '0' ... '0' '1' '02-02-2020']\n",
      " ['湖北省' '24953' '0' ... '2' '0' '02-08-2020']\n",
      " ...\n",
      " ['湖北省' '51986' '0' ... '504' '59' '02-14-2020']\n",
      " ['湖北省' '54406' '0' ... '92' '15' '02-15-2020']\n",
      " ['湖北省' '9074' '0' ... '175' '224' '02-02-2020']]\n"
     ]
    }
   ],
   "source": [
    "# (2)用Numpy内置函数查找湖北省的数据，并对数据进行去重\n",
    "provinceName='湖北省'\n",
    "hbdata=data[np.where(data==provinceName)[0]]\n",
    "# 去重\n",
    "hbdata=np.array(list(set([tuple(t) for  t in hbdata])))\n",
    "print(hbdata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['河南省' '1231']\n",
      " ['台湾' '20']\n",
      " ['江西省' '925']\n",
      " ['内蒙古自治区' '70']\n",
      " ['天津市' '124']\n",
      " ['辽宁省' '121']\n",
      " ['澳门' '10']\n",
      " ['四川省' '481']\n",
      " ['重庆市' '547']\n",
      " ['陕西省' '236']\n",
      " ['江苏省' '617']\n",
      " ['辽宁省' '120']\n",
      " ['上海市' '328']\n",
      " ['河北省' '300']\n",
      " ['贵州省' '144']\n",
      " ['安徽省' '962']\n",
      " ['云南省' '171']\n",
      " ['新疆维吾尔自治区' '71']\n",
      " ['湖北省' '56249']\n",
      " ['云南省' '169']\n",
      " ['广西壮族自治区' '237']\n",
      " ['香港' '57']\n",
      " ['重庆市' '544']\n",
      " ['浙江省' '1167']\n",
      " ['福建省' '287']\n",
      " ['山西省' '128']\n",
      " ['上海市' '326']\n",
      " ['黑龙江省' '445']\n",
      " ['山东省' '537']\n",
      " ['海南省' '162']\n",
      " ['广东省' '1316']\n",
      " ['湖南省' '1004']\n",
      " ['甘肃省' '90']\n",
      " ['北京市' '380']\n",
      " ['吉林省' '89']\n",
      " ['香港' '56']]\n"
     ]
    }
   ],
   "source": [
    "# （3）获取2020年2月16日各个省份的确诊人数\n",
    "provinceConfirmCount=timedata[:,0:2]\n",
    "provinceConfirmCount=np.array(list(set([tuple(t) for  t in provinceConfirmCount])))\n",
    "print(provinceConfirmCount)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 5、数据的统计分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1231    20   925    70   124   121    10   481   547   236   617   120\n",
      "   328   300   144   962   171    71 56249   169   237    57   544  1167\n",
      "   287   128   326   445   537   162  1316  1004    90   380    89    56]\n",
      "262.0\n",
      "1936.6944444444443\n",
      "69721\n"
     ]
    }
   ],
   "source": [
    "province_count=provinceConfirmCount[:,1:].astype(np.int32).reshape(-1)\n",
    "print(province_count)\n",
    "# 计算中位数\n",
    "print(np.median(province_count))\n",
    "#计算算数平均值\n",
    "print(np.mean(province_count))\n",
    "# 计算总和\n",
    "print(np.sum(province_count))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
